--trade crystal

--the price that one crystal need,must be integer
local crystal_buy_price = 50000
local crystal_sell_price = 2500

local crystal_count_new = 0
local gold_count_new = 0
local success = false

local crystal_selected = 0
-----------------------
-- Generate object hint
-----------------------
function get_crystal_buy_price()
	local label = tostring(crystal_buy_price)
	return label
end

function get_crystal_sell_price()
	local label = tostring(crystal_sell_price)
	return label
end

function get_crystal_trade_count()
	local crystal_count = get_crystal_count()
	return "<align=center><image=Quests_icon_crystal.png> = "..tostring(crystal_count)
end

function get_crystal_buy_exit()
    if get_gold_count() < crystal_buy_price then
      return "<label=money_not_enough>"
	else
	  return "<label=dlg_exit>"
	end
end

function get_crystal_sell_exit()
    if get_crystal_count() == 0 then
      return "<label=crystal_not_enough>"
	else
	  return "<label=dlg_exit>"
	end
end

function get_crystal_trade_choice()

	local crystal_num,crystal_price = get_crystal_trade()
    if crystal_selected == 1 or crystal_selected ==2 or crystal_selected ==3 then
       lable = "<label=trade_buy>".."<image=Quests_icon_money.png>"..tonumber(crystal_price).."<label=buy>"..crystal_num.."<image=Quests_icon_crystal.png>".."<label=crystal>"
    elseif crystal_selected == 4 or crystal_selected ==5 or crystal_selected ==6 then
       lable = "<label=trade_sell>".."<image=Quests_icon_money.png>"..tonumber(crystal_price).."<label=sell>"..crystal_num.."<image=Quests_icon_crystal.png>".."<label=crystal>"
	end
    return lable
end

function get_crystal_trade()
    if crystal_selected == 1 or crystal_selected == 4 then
	    if crystal_selected == 1 then
		   return "<label=num1>",crystal_buy_price
	    else
	       return "<label=num1>",crystal_sell_price
		end
    elseif crystal_selected == 2 or crystal_selected == 5 then
        if crystal_selected == 2 then
		   return "<label=num5>",crystal_buy_price*5
	    else
	       return "<label=num5>",crystal_sell_price*5
		end
	elseif crystal_selected == 3 or crystal_selected == 6 then
        if crystal_selected == 3 then
		   return "<label=num10>",crystal_buy_price*10
	    else
	       return "<label=num10>",crystal_sell_price*10
		end
	end
end

------------------------------
-- trade the selected crystals
------------------------------
function crystal_trade_chosen_crystals(choice_number)
    success = false
	-- Sanity check
	if choice_number >= 1 and choice_number <= 6 then

		-- Get the number of crystals
		local crystal_count = get_crystal_count()
		local gold_count = get_gold_count()
		-- Init new amount of crystals
		crystal_count_new = crystal_count
		gold_count_new = gold_count

		-- Check what the user's choice was
		if choice_number == 1 and gold_count>=crystal_buy_price then
			-- buy one crystal
			crystal_count_new = crystal_count + 1
			gold_count_new = gold_count - crystal_buy_price
			success=true
		elseif choice_number == 2 and gold_count>=crystal_buy_price*5 then
			-- buy five crystals
			crystal_count_new = crystal_count + 5
			gold_count_new = gold_count - crystal_buy_price*5
			success=true
		elseif choice_number == 3 and gold_count>=crystal_buy_price*10 then
			-- buy ten crystals
			crystal_count_new = crystal_count + 10
			gold_count_new = gold_count - crystal_buy_price*10
			success=true
		elseif choice_number == 4 and crystal_count >= 1 then
			-- sell one crystal
			crystal_count_new = crystal_count - 1
			gold_count_new = gold_count + crystal_sell_price
			success=true
		elseif choice_number == 5 and crystal_count >= 5 then
			-- sell five crystals
			crystal_count_new = crystal_count - 5
			gold_count_new = gold_count + crystal_sell_price*5
			success=true
		elseif choice_number == 6 and crystal_count >= 10 then
			-- sell ten crystals
			crystal_count_new = crystal_count - 10
			gold_count_new = gold_count + crystal_sell_price*10
			success=true
		end
	end
end
-----------------------------------------------
-- Conditions for displaying the dialog options
-----------------------------------------------
function crystal_dlg_condition_n(n)
	local success=false
	Logic.hero_lu_var("crystal_trade_dlg",0)
    if n==1 then
		if get_gold_count() >= crystal_buy_price then
			success=true
		end
    elseif n==2 then
	    if get_gold_count() >= crystal_buy_price*5 then
			success=true
		end
    elseif n==3 then
	    if get_gold_count() >= crystal_buy_price*10 then
			success=true
		end
	elseif n==4 then
		if get_crystal_count() >= 1 then
			success=true
		end
	elseif n==5 then
	    if get_crystal_count() >= 5 then
			success=true
		end
	elseif n==6 then
	    if get_crystal_count() >= 10 then
			success=true
		end
	end
	if success then
	   Logic.hero_lu_var("crystal_trade_dlg",n)
	end
	return success
end


function crystal_dlg_condition1(param) --ftag:if
	return crystal_dlg_condition_n(1)
end

function crystal_dlg_condition2(param) --ftag:if
	return crystal_dlg_condition_n(2)
end

function crystal_dlg_condition3(param) --ftag:if
	return crystal_dlg_condition_n(3)
end

function crystal_dlg_condition4(param) --ftag:if
	return crystal_dlg_condition_n(4)
end

function crystal_dlg_condition5(param) --ftag:if
	return crystal_dlg_condition_n(5)
end

function crystal_dlg_condition6(param) --ftag:if
	return crystal_dlg_condition_n(6)
end

--------------------------------------------------------
-- Store the user's choice in the dialog and force usage
--------------------------------------------------------
function crystal_dlg_use_x(x)
    crystal_trade_chosen_crystals(x)
	return true
end

function crystal_dlg_use1(param) --ftag:action
    crystal_selected = 1
	return crystal_dlg_use_x(1)
end

function crystal_dlg_use2(param) --ftag:action
    crystal_selected = 2
	return crystal_dlg_use_x(2)
end

function crystal_dlg_use3(param) --ftag:action
    crystal_selected = 3
	return crystal_dlg_use_x(3)
end

function crystal_dlg_use4(param) --ftag:action
    crystal_selected = 4
	return crystal_dlg_use_x(4)
end

function crystal_dlg_use5(param) --ftag:action
    crystal_selected = 5
	return crystal_dlg_use_x(5)
end

function crystal_dlg_use6(param) --ftag:action
    crystal_selected = 6
	return crystal_dlg_use_x(6)
end

function crystal_dlg_choice(param)
	if success then
		Logic.hero_lu_item("crystals", "count", crystal_count_new)
		Logic.hero_lu_item("money","count",gold_count_new)
	end
end

